//本js 主要是用于调用定位js 的。
import {config} from "../config.js"
import {place} from "../api/common.js"
// #ifdef H5
import {ybLocation} from "./yb.js"
// #endif

export function getLonLat(){
	// #ifdef H5
	return new Promise((resolve,reject)=>{
		ybLocation().then(res=>{
			res = JSON.parse(res)
			resolve(res)
		}).catch(err=>{
			uniLonLat().then(res=>{
				resolve(res)
			}).catch(err=>{
				reject(err)
			})
		})
	});
	// #endif
	return uniLonLat();
}


export function getLocation(){
	// #ifdef H5
	return new Promise((resolve,reject)=>{
		ybLocation().then(res=>{
			res = JSON.parse(res)
			getPlace(res.latitude,res.longitude,res.address).then(address=>{
				res.address = address
				resolve(res)
			}).catch(err=>{
				reject(err)
			})
		}).catch(err=>{
			if(err==="switch"){
				uniLocation().then(res=>{
					resolve(res)
				}).catch(err=>{
					reject(err)
				})
			}else{
				reject(err)
			}
		})
	});
	// #endif
	
	// #ifndef H5
		return uniLocation();	
	// #endif
}


function uniLonLat(){
	return new Promise((resolve,reject)=>{
		uni.getLocation({
			altitude:true,
		    type: 'gcj02',
		    success: function (res) {
				res.latitude = res.latitude //Number(res.latitude.toFixed(4))
				res.longitude = res.longitude//Number(res.longitude.toFixed(4))
				resolve(res)
		    }
		});
	})
}

function uniLocation(){
	return new Promise((resolve,reject)=>{
		uni.getLocation({
			altitude:true,
		    type: 'gcj02',
		    success: function (res) {
				res.latitude = res.latitude //Number(res.latitude.toFixed(4))
				res.longitude = res.longitude//Number(res.longitude.toFixed(4))
				getPlace(res.latitude,res.longitude).then(address=>{
					res.address = address
					resolve(res)
				}).catch(err=>{
					reject(err)
				})
		    }
		});
	})
}

function getPlace(lat,lon,address){
	//当不在H5环境中 无须解决跨域
	// #ifndef H5
	var url = "https://apis.map.qq.com/ws/geocoder/v1/?location="+lat+","+lon+"&key="+config.location.TX_KEY
	return new Promise((resolve,reject)=>{
		if (address){
			resolve(address)
			return
		}
		uni.request({
			url:url,
			success:res=>{
				if(res.data.status===0){
					resolve(res.data.result.address)
				}else{
					reject(res.data.message)
				}
			},
			fail() {
				reject("定位地址解析失败")
			}
		})
	}) 
	// #endif
	
	// #ifdef H5
		return new Promise((resolve,reject)=>{	
			console.log(address)
			if (address){
				resolve(address)
				return
			}
			place({lat:lat,lon:lon}).then(res=>{
				console.log("place")
				if(res.status===0){
					resolve(res.result.address)
				}else{
					reject(res.message)
				}
			})
		})
	// #endif
	
}

var EARTH_RADIUS = 6378137.0; //单位M
var PI = Math.PI;
 
function getRad(d) {
  return d * PI / 180.0;
}

export function distance(lat1,lng1,lat2,lng2){
    var f = getRad((lat1 + lat2)/2);
    var g = getRad((lat1 - lat2)/2);
    var l = getRad((lng1 - lng2)/2);
    
    var sg = Math.sin(g);
    var sl = Math.sin(l);
    var sf = Math.sin(f);
    
    var s,c,w,r,d,h1,h2;
    var a = EARTH_RADIUS;
    var fl = 1/298.257;
    
    sg = sg*sg;
    sl = sl*sl;
    sf = sf*sf;
    
    s = sg*(1-sl) + (1-sf)*sl;
    c = (1-sg)*(1-sl) + sf*sl;
    
    w = Math.atan(Math.sqrt(s/c));
    r = Math.sqrt(s*c)/w;
    d = 2*w*a;
    h1 = (3*r -1)/2/c;
    h2 = (3*r +1)/2/s;
    
    return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
}